<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Orders extends Base{
	//下单
	public function place_order(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        if(!empty(input("param.money"))){
            $data['money']=input("param.money");
        }else{
            return json(['code' => 502, 'msg' => '金额不能为空']);exit();
        }
        if(!empty(input("param.pro_id"))){
            $data['pro_id']=input("param.pro_id");
            $product=Db::name('product')->where(array('id'=>$data['pro_id']))->find();
            if($product){
                if($data['money']<$product['moneyf']||$data['money']>$product['moneyt']){
                    return json(['code' => 505, 'msg' => '金额不在范围内']);exit();
                }
                // if($data['money']>$product['sharemoney']){
                //     return json(['code' => 506, 'msg' => '剩余金额不足']);exit();
                // }
                $data['cus_id']=$product['cus_id'];
            }else{
                return json(['code' => 504, 'msg' => '产品不存在']);exit();
            }
        }else{
            return json(['code' => 503, 'msg' => '产品不能为空']);exit();
        }
        $data['user_id']=$this->do_mdecrypt($token);
        $orstate=Db::name('orders')->where('user_id',$data['user_id'])->where(['state'=>[1,3,11,17,19]])->find();
        if($orstate['state']==1){
            return json(['code' => 506, 'msg' => '存在申请订单，不能重复申请']);exit();
        }elseif($orstate['state']==3){
            return json(['code' => 507, 'msg' => '存在用户确认放款订单，不能重复申请']);exit();
        }elseif($orstate['state']==11){
            return json(['code' => 508, 'msg' => '存在待放款订单，不能重复申请']);exit();
        }elseif($orstate['state']==17){
            return json(['code' => 509, 'msg' => '存在待还款订单，不能重复申请']);exit();
        }elseif($orstate['state']==19){
            return json(['code' => 510, 'msg' => '存在逾期订单，请先保证银行卡余额充足还款后再申请']);exit();
        }
        $data['order_no']=date('YmdHis',time()).mt_rand(111,999);
        $data['addtime']=time();
        $data['state']=1;
        $res=Db::name('orders')->insert($data);
        if($res){
            // $sharemoney=$product['sharemoney']-$data['money'];
            // Db::name('product')->where(array('id'=>$data['pro_id']))->update(['sharemoney'=>$sharemoney]);
            return json(['code' => 200, 'msg' => '借款成功' ,'data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '借款失败']);exit();
        }
	}
    //个人全部订单列表
    public function per_orders(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        $user_id=$this->do_mdecrypt($token);
        $res=Db::name('orders')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->join('product c', 'a.pro_id = c.id','LEFT')->field('a.*,b.cname,c.title')->where(array('a.user_id'=>$user_id))->order('a.id desc')->select();
        if($res){
            for ($i=0; $i <count($res) ; $i++) { 
                $res[$i]['addtime']=date('Y-m-d H:i:s',$res[$i]['addtime']);
                $res[$i]['uptime']=date('Y-m-d H:i:s',$res[$i]['uptime']);
            }
            return json(['code' => 200, 'msg' => '成功','data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //待审订单列表
    public function per_ordering(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        $user_id=$this->do_mdecrypt($token);
        // $res=Db::name('orders')->where(array('user_id'=>$user_id,'state'=>1))->order('id desc')->select();
        $res=Db::name('orders')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->join('product c', 'a.pro_id = c.id','LEFT')->field('a.*,b.cname,c.title')->where(array('a.user_id'=>$user_id))->where(array('user_id'=>$user_id,'state'=>1))->order('a.id desc')->select();
        if($res){
            for ($i=0; $i <count($res) ; $i++) { 
                $res[$i]['addtime']=date('Y-m-d H:i:s',$res[$i]['addtime']);
                $res[$i]['uptime']=date('Y-m-d H:i:s',$res[$i]['uptime']);
            }
            return json(['code' => 200, 'msg' => '成功','data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //待确认订单列表
    public function per_order_confir(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        $user_id=$this->do_mdecrypt($token);
        // $res=Db::name('orders')->where(array('user_id'=>$user_id,'state'=>3))->order('id desc')->select();
        $res=Db::name('orders')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->join('product c', 'a.pro_id = c.id','LEFT')->field('a.*,b.cname,c.title')->where(array('a.user_id'=>$user_id))->where(array('user_id'=>$user_id,'state'=>3))->order('a.id desc')->select();
        if($res){
            for ($i=0; $i <count($res) ; $i++) { 
                $res[$i]['addtime']=date('Y-m-d H:i:s',$res[$i]['addtime']);
                $res[$i]['uptime']=date('Y-m-d H:i:s',$res[$i]['uptime']);
            }
            return json(['code' => 200, 'msg' => '成功','data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //待确认之用户确认
    public function order_confirm(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 502, 'msg' => '订单id不能为空']);exit();
        }
        if(input("param.state")==9){
            $state=9;
        }elseif(input("param.state")==11){
            $state=11;
        }else{
            return json(['code' => 503, 'msg' => '要么确认要么放弃']);exit();
        }
        $user_id=$this->do_mdecrypt($token);
        $res=Db::name('orders')->where(array('id'=>$order_id,'user_id'=>$user_id))->update(['state'=>$state]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //还款中订单列表
    public function per_order_ing(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);exit();
        }
        $user_id=$this->do_mdecrypt($token);
        // $res=Db::name('orders')->where(array('user_id'=>$user_id,'state'=>17))->order('id desc')->select();
        $res=Db::name('orders')->alias('a')->join('customer b', 'a.cus_id = b.id','LEFT')->join('product c', 'a.pro_id = c.id','LEFT')->field('a.*,b.cname,c.title')->where(array('a.user_id'=>$user_id))->where(array('user_id'=>$user_id,'state'=>[17,19]))->order('a.id desc')->select();
        if($res){
            for ($i=0; $i <count($res) ; $i++) { 
                $res[$i]['addtime']=date('Y-m-d H:i:s',$res[$i]['addtime']);
                $res[$i]['uptime']=date('Y-m-d H:i:s',$res[$i]['uptime']);
            }
            return json(['code' => 200, 'msg' => '成功','data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //个人订单分期详情
    public function per_orders_detail(){
        if(!empty(input("param.order_id"))){
            $order_id=input("param.order_id");
        }else{
            return json(['code' => 501, 'msg' => '订单id不能为空']);exit();
        }
        $res=Db::name('orderdetail')->alias('a')->join('orders b', 'a.order_id = b.id','LEFT')->join('product c', 'b.pro_id = c.id','LEFT')->join('customer d', 'b.cus_id = d.id','LEFT')->field('a.*,b.money as moneys,b.cycle as cycles,b.addtime as xdtime,b.fktime,b.httime,c.title,d.cname')->where(array('a.order_id'=>$order_id))->order('a.id asc')->select();
        if($res){
            for ($i=0; $i <count($res) ; $i++) { 
                $res[$i]['xdtime']=date('Y-m-d H:i:s',$res[$i]['xdtime']);
                $res[$i]['fktime']=date('Y-m-d H:i:s',$res[$i]['fktime']);
                $res[$i]['httime']=date('Y-m-d H:i:s',$res[$i]['httime']);
            }
            return json(['code' => 200, 'msg' => '成功','data' =>$res]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败']);exit();
        }

    }
    //解密
    private function do_mdecrypt($string, $key=''){
        $key= !empty($key) ? $key : config('byzk.KEY');
        $operation = 'D';
        $key=md5($key); 
        $key_length=strlen($key); 
        $string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string; 
        $string_length=strlen($string); 
        $rndkey=$box=array(); 
        $result=''; 
        for($i=0;$i<=255;$i++){ 
            $rndkey[$i]=ord($key[$i%$key_length]); 
            $box[$i]=$i; 
        } 
        for($j=$i=0;$i<256;$i++){ 
            $j=($j+$box[$i]+$rndkey[$i])%256; 
            $tmp=$box[$i]; 
            $box[$i]=$box[$j]; 
            $box[$j]=$tmp; 
        } 
        for($a=$j=$i=0;$i<$string_length;$i++){ 
            $a=($a+1)%256; 
            $j=($j+$box[$a])%256; 
            $tmp=$box[$a]; 
            $box[$a]=$box[$j]; 
            $box[$j]=$tmp; 
            $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256])); 
        } 
        if($operation=='D'){ 
            if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){ 
                return substr($result,8); 
            }else{ 
                return''; 
            } 
        }else{ 
            return str_replace('=','',base64_encode($result)); 
        } 
    }
}